<%@page import="edu.mta.pokerstats.Game"%>
<%@page import="edu.mta.pokerstats.GameRecord"%>
<%@page import="java.util.ArrayList"%>
<%@page import="edu.mta.pokerstats.Player"%>
<%@page import="edu.mta.pokerstats.Team"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
int viewedGameID = Integer.parseInt(request.getParameter("gameID"));
Player viewingPlayer = (Player)session.getAttribute("viewingPlayer");
Game viewedGame = Game.getGameById(viewedGameID);
Team gameTeam = Team.getTeamById(viewingPlayer.getTeamId());
ArrayList<GameRecord> recordsList = viewedGame.GetRecordsForGame();

if (viewingPlayer.isTeamLeader()){
%>
<script type="text/javascript" src="js/editDeleteGameRecords.js"></script>
<%
}
%>
<script type="text/javascript">
	$(document).ready(function () {
		$('#GameRecordsTable').dataTable({
			"iDisplayLength": 50,
		    "aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
			"aaSorting": [[2,"asc"]],
			"aoColumnDefs": [
					{ 'bSortable': false, 'aTargets': [7] },
					{ 'bSearchable': false, 'aTargets': [7]}
			]
		});
	} );

	function displayPlayer(i_PlayerID){
		$.ajax({
			url : "ShowSinglePlayerRecords.jsp",
			type : "POST",
			data : {
				playerID : i_PlayerID
			},
			success : function(data, status) {
				changeDivMainContainer(data);
			}
		});
	}
	</script>

<h3>Details for Game of Team "<%=gameTeam.getName()%>"</h3>
Date : <%=viewedGame.getDate()%>.<br>
Buy-In cost : <%=viewedGame.getCostOfBuyIn()%>.<br> 	
Rebuy cost : <%=viewedGame.getCostOfRebuy()%>.<br>
<%
	if (viewedGame.getRemark() != ""){
%>
Remark : <%=viewedGame.getRemark()%>.<br>
<%
	}
%>
<%
if (viewingPlayer.isTeamLeader()){
%>
<script type="text/javascript" src="js/createNewGameRecord.js"></script>
<button class="yellowButton largeBtn" id="buttonEnterNewRecord" onclick="enableCreateNew()">Enter new record</button>
<div id="divCreateNewGameRecord" hidden="hidden">
	<h4>Fill out this form and submit to enter new record to the system.</h4>
	<div>
		<label>Select Player:</label><br/>
		<select id="recordPlayer">
		<% for (Player player : Team.getTeamById(viewingPlayer.getTeamId()).getPlayersInTeam()){%>
			<option value="<%=player.getId()%>"><%=player.getName()%></option>
		<%}%>
		</select>
	</div>
	<div>
		<label>Number of Rebuys:</label><br/>
		<input type="number" id="recordRebuys" min="0" value="0">
		<a id="RebuysError" class="inputError" hidden="hidden">Please Enter a valid number.</a>
	</div>
	<div>
		<label>Amount won in Game:</label><br/>
		<input type="number" id="recordWinnings" min="0" value="0">
		<a id="WinningsError" class="inputError" hidden="hidden">Please Enter a valid number.</a>
	</div>
	<div>
		<label>Position in Game:</label><br/>
		<input type="number" id="recordPosition" min="1" value="1">
		<a id="PositionError" class="inputError" hidden="hidden">Please Enter a valid number.</a>
	</div>
	<div>
		<label>Knockouts in Game:</label><br/>
		<input type="number" id="recordKnockouts" min="0" value="0">
		<a id="KnockoutsError" class="inputError" hidden="hidden">Please Enter a valid number.</a>
	</div>
	<br/>
	<button class="yellowButton largeBtn" id="recordSubmit" onclick="submitNewGameRecord(<%=viewedGameID%>)">Submit</button>
	<button class="yellowButton largeBtn" id="recordCancel" onclick="cancelNewGameRecord()">Cancel</button>
</div>
<div id="submitResult"></div>
<%
}
%>
<table id="GameRecordsTable" class="display">
	<thead>
		<tr>
			<th>Player Name</th>
			<th>Number Of Rebuys</th>
			<th>Position</th>
			<th>Knockouts</th>
			<th>Winnings</th>
			<th>Investment</th>
			<th>Balance</th>
			<th>Actions</th>
		</tr>
	</thead>
	<tbody>
	<%
		for (int i = 0; i < recordsList.size(); i++) {
			GameRecord currRecord = recordsList.get(i);
			Player currPlayer = Player.getPlayerById(currRecord.getPlayerId());
			int invested = viewedGame.GetInvestment(currRecord.getNumberOfRebuys());
			int winnings = currRecord.getWonInGame();
			int balance = winnings - invested;
	%>
		<tr id="row<%=i + 1%>">
			<td><%=currPlayer.getName()%></td>
			<td id="row<%=i + 1%>Rebuys"><div><%=currRecord.getNumberOfRebuys()%></div></td>
			<td id="row<%=i + 1%>Position"><div><%=currRecord.getPosition()%></div></td>
			<td id="row<%=i + 1%>Knockouts"><div><%=currRecord.getKnockouts()%></div></td>
			<td id="row<%=i + 1%>Winnings"><div><%=winnings%></div></td>
			<td id="row<%=i + 1%>Invested"><%=invested%></td>
			<td id="row<%=i + 1%>Balance"><%=balance%></td>
			<td>
		<%
				if (viewingPlayer.isTeamLeader()) {
		%>
				<button id="row<%=i + 1%>EditBtn" onclick="editGameRecord(<%=i + 1%>,<%=currRecord.getGameId()%>,<%=currRecord.getPlayerId()%>)">Edit</button>
				<button id="row<%=i + 1%>DeleteBtn" onclick="deleteGameRecord(<%=i + 1%>,<%=currRecord.getGameId()%>,<%=currRecord.getPlayerId()%>)">Delete</button>

				<button id="row<%=i + 1%>SaveBtn" hidden="hidden" onclick="saveEditGameRecord(<%=i + 1%>,<%=currRecord.getGameId()%>,<%=currRecord.getPlayerId()%>)">Save</button>
				<button id="row<%=i + 1%>CancelBtn" hidden="hidden" onclick="cancelEditGameRecord(<%=i + 1%>,<%=currRecord.getGameId()%>,<%=currRecord.getPlayerId()%>)">Cancel</button>
		<%
				}
		%>
				<button onclick="displayPlayer(<%=currPlayer.getId()%>)">View Player Info</button>
			</td>
		</tr>
	<%
		}
	%>
	</tbody>
</table>